package com.fjh.impl;

import com.fjh.dao.BorrowDao;
import com.fjh.pojo.Borrow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class BorrowDaoImpl implements BorrowDao {
    private final JdbcTemplate jdbcTemplate;

    @Autowired
    public BorrowDaoImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public void borrowBook(Borrow borrow) {
        String borrowSql = "INSERT INTO borrow (book_id, user_id, borrow_date) VALUES (?, ?, NOW())";
        jdbcTemplate.update(borrowSql, borrow.getBookId(), borrow.getUserId());
    }

    @Override
    public void returnBook(int borrowId) {
        String returnSql = "UPDATE borrow SET return_date=NOW() WHERE id=?";
        jdbcTemplate.update(returnSql, borrowId);
    }
}